home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Source Code ƒ / MPW C ƒ / chgfntƒ / changefont.c next >
Encoding:
C/C++ Source or Header  |  1989-10-02  |  7.5 KB  |  272 lines  |  [TEXT/MPS ]

  1. /*--------------------------------------------------------------------------*
  2.  *        changefont --    an MPW tool to perform batch formatting on            *
  3.  *                        Think C files.                                        *
  4.  *        Copyright © 1989 by Carlos A. Weber, M.D.                            *
  5.  *        Syntax    :    changefont -f fontname -s size -t tabs [filenames...]    *
  6.  *                    defaults to Courier 10, tab setting 4                    *
  7.  *--------------------------------------------------------------------------*/
  8.  
  9. #include    <Types.h>
  10. #include     <fcntl.h>
  11. #include     <stdio.h>
  12. #include    <ErrMgr.h>
  13. #include    <CursorCtl.h>
  14. #include    <Errors.h>
  15. #include    <Resources.h>
  16. #include    <Files.h>
  17. #include    <Quickdraw.h>
  18. #include    <Windows.h>
  19. #include    <Fonts.h>
  20. #include    <Memory.h>
  21.  
  22. #define    SPACE    0xCA    /* ASCII value for non-breaking space character */
  23. #define    EFF        0x66    /* ASCII value for 'f' */
  24. #define    ESS        0x73    /* ASCII value for 's' */
  25. #define    TEE        0x74    /* ASCII value for 't' */
  26. #define    DEFSZ    10        /* 10 point type unless user chooses otherwise    */
  27. #define    DEFTAB    4        /* tab size defaults to 4    */
  28.  
  29. typedef struct efntData {
  30.     short    fSize;
  31.     char    fName[256];
  32. } efntData;                /* structure of the EFNT resource type */
  33.  
  34. typedef struct etabData {
  35.     short    mystery;
  36.     short    tSize;
  37. } etabData;                /* data structure for the ETAB resource */
  38.  
  39. static char        defFont[] = "\pCourier";    /* default font if not overridden */
  40.  
  41. static efntData    myEFNT;
  42. static etabData    myETAB;
  43.  
  44. void SyntaxError( short err, char *msg)
  45. {
  46.     switch (err)
  47.     {
  48.         case 1:    printf("# %s is an invalid option\n", msg);
  49.                 break;
  50.         case 2:    printf("# missing font\n");
  51.                 break;
  52.         case 3:    printf("# missing font size\n");
  53.                 break;
  54.         case 4:    printf("# missing tab setting\n");
  55.                 break;
  56.         case 5:    printf("# %s is an invalid font\n", msg);
  57.                 break;
  58.         case 6:    printf("# %s is an invalid font size\n", msg);
  59.                 break;
  60.         case 7:    printf("# %s is an invalid tab size\n", msg);
  61.                 break;
  62.         case 8:    printf("# the - character must be accompanied by an option\n");
  63.                 break;
  64.         case 9:    printf("# Usage -- changefont [filenames…]   \n");
  65.                 printf("        -f fontname    # set font of filenames to fontname\n");
  66.                 printf("        -s fontsize    # set font size of filenames to fontsize\n");
  67.                 printf("        -t tabs        # set tab setting to tabs\n");
  68.                 break;
  69.     }
  70.     exit(1);
  71. }
  72.  
  73. void HandleOption(char *opt, short *argIndex, int argc, char *argv[])
  74. {
  75.     char    optionChar = tolower(opt[1]);
  76.     short    fNum;
  77.     long    biggy;
  78.     
  79.     switch((short)optionChar)
  80.     {
  81.         case    EFF :    
  82.             if (++(*argIndex) < argc)
  83.             {
  84.                 getfnum(argv[*argIndex], &fNum); /* use lower case, pass C string*/
  85.                 if (fNum <= 0)
  86.                     SyntaxError(5, argv[*argIndex]);
  87.                 else
  88.                     strcpy(myEFNT.fName, c2pstr(argv[*argIndex]));
  89.             }
  90.             else
  91.                 SyntaxError(2, "");
  92.             break;
  93.         case    ESS :
  94.             if (++(*argIndex) < argc)
  95.             {
  96.                 stringtonum(argv[*argIndex], &biggy); /* use lower case, pass C string*/
  97.                 if (biggy <= 0 || biggy >= 128)                
  98.                     SyntaxError(6, argv[*argIndex]);
  99.                 else
  100.                     myEFNT.fSize = biggy;
  101.             }
  102.             else
  103.                 SyntaxError(3, "");    
  104.             break;
  105.         case    TEE :
  106.             if (++(*argIndex) < argc)
  107.             {
  108.                 stringtonum(argv[*argIndex], &biggy); /* use lower case, pass C string*/
  109.                 if (biggy <= 0 || biggy >= 25)
  110.                     SyntaxError(7, argv[*argIndex]);
  111.                 else
  112.                     myETAB.tSize = biggy;
  113.             }
  114.             else
  115.                 SyntaxError(4, "");    
  116.             break;
  117.         default    :
  118.             SyntaxError(1, opt);
  119.             break;
  120.     }        
  121. }
  122.  
  123. void SkipOption(char *opt, short *argIndex)
  124. {
  125.     char    optionChar = tolower(*(opt+1));
  126.     
  127.     if (optionChar == EFF || optionChar == ESS || optionChar ==  TEE)
  128.         ++(*argIndex);
  129. }
  130.  
  131. void ReadCommandLine(int argc, char *argv[])
  132. {
  133.     short    argVIndex;
  134.     size_t    strSize;
  135.     char    curArg[256];
  136.     
  137.     if (argc == 1)
  138.         SyntaxError(9,"");
  139.     for (argVIndex = 1; argVIndex < argc; argVIndex++)
  140.     {
  141.         strSize = strlen(strcpy(curArg,argv[argVIndex]));
  142.         if (curArg[0] == '-')
  143.         {
  144.             if (strSize > 1)
  145.                 HandleOption(curArg, &argVIndex, argc, argv);
  146.             else
  147.                 SyntaxError(8, "");
  148.             
  149.         }        
  150.     }
  151. }
  152.  
  153. void    ReportError(short error, char *filename)
  154. {
  155.     if (error == noErr) return;  /* don't exit for non-errors */
  156.     fprintf(stderr, "ERROR!!  ");
  157.     switch (error)
  158.     {
  159.         case -35 :    fprintf(stderr, "%s volume does not exist\n", filename);
  160.                     break;
  161.         case -36 :    fprintf(stderr, "%s IO Error\n", filename);
  162.                     break;
  163.         case -37 :    fprintf(stderr, "%s is a bad file or volume name\n", filename);
  164.                     break;
  165.         case -42 :    fprintf(stderr, "Too many files open\n");
  166.                     break;
  167.         case -43 :    fprintf(stderr, "%s not found\n", filename);
  168.                     break;
  169.         case -45 :    fprintf(stderr, "%s is locked\n", filename);
  170.                     break;
  171.         case -46 :    fprintf(stderr, "%s is locked by a software flag\n", filename);
  172.                     break;
  173.         case -47 :    fprintf(stderr, "%s is busy; one or more files are open\n", filename);
  174.                     break;
  175.         case -53 :    fprintf(stderr, "%s volume not on-line\n", filename);
  176.                     break;
  177.         case -54 :    fprintf(stderr, "%s cannot be opened for writing; file is locked\n", filename);
  178.                     break;
  179.         case -61 :    fprintf(stderr, "%s Read/write permission does not allow writing\n", filename);
  180.                     break;
  181.         default:    fprintf(stderr, "OS error #%d has occurred.\n", error);
  182.                     break;                    
  183.     }
  184.     exit(2);
  185. }
  186.  
  187. short    spaceWidth(void)    /* first word of ETAB resource is width of a    *
  188.                              * non-breaking space                            */
  189. {
  190.     short        result, fontNum;
  191.     WindowPtr    dummy;
  192.     Rect        dRect;
  193.         
  194.     SetRect(&dRect, 5000, 5000, 5050, 5050);
  195.     dummy = NewWindow(nil, &dRect, "\p", false, noGrowDocProc, (WindowPtr)-1, false, 0L);
  196.     SetPort(dummy);        /* all this to get a GrafPort to pseudo-draw in    */
  197.     getfnum(myEFNT.fName, &fontNum);
  198.     TextFont(fontNum);    /* using our offscreen invisible window    */
  199.     TextSize(myEFNT.fSize);
  200.     result = CharWidth(SPACE);
  201.     DisposeWindow(dummy);
  202.     return result;
  203. }
  204.  
  205. void main(int argc, char *argv[])
  206. {
  207.     short        resRefNum, index, efntSize;
  208.     FInfo        skinny;
  209.     OSErr        goof;
  210.     Handle        myETHdl, myEFHdl;
  211.     short        numFiles = 0;
  212.     
  213.     InitGraf((Ptr) &qd.thePort);     /* for Window Manager calls in spaceWidth()    */
  214.     SetFScaleDisable(true);            /* per the MPW Manual    */
  215.     InitCursorCtl(nil);
  216.     /*SetResLoad(false);*/
  217.     
  218.     myETAB.tSize = DEFTAB;            /* set default values for resource fields    */
  219.     myEFNT.fSize = DEFSZ;
  220.     strcpy(myEFNT.fName, defFont);
  221.     
  222.     ReadCommandLine(argc,argv);        /* see if they get overridden by user    */
  223.     
  224.     myETAB.mystery = spaceWidth();    /* set this based on actual font & size    */
  225.     
  226.     for (index = 1; index < argc; index++)
  227.     {
  228.         RotateCursor(32);
  229.         if (strlen(argv[index]) == 0)    /* how could this happen?    */
  230.             continue;
  231.         if (*argv[index] == '-')    /* it's an option    */
  232.         {
  233.             SkipOption(argv[index], &index);    /* so skip the next arg as well    */
  234.             continue;
  235.         }
  236.         else        /* it should be a filename    */
  237.         {
  238.             if (goof = getfinfo(argv[index], 0, &skinny))    /* not noErr?    */
  239.             {
  240.                 ReportError(goof, argv[index]);
  241.                 continue;
  242.             }
  243.             else    /* goof was zero    */
  244.             {
  245.                 if (skinny.fdType == 'TEXT' && skinny.fdCreator == 'KAHL')    
  246.                 {    /* correct type & creator?    */
  247.                     if (openrf(argv[index], 0, &resRefNum) == noErr)
  248.                     {    /* a resource fork already existed, so wipe it out    */
  249.                         goof = SetEOF(resRefNum, 0);
  250.                         goof = FSClose(resRefNum);
  251.                     }
  252.                     createresfile(argv[index]);                /* start fresh    */
  253.                     resRefNum = openresfile(argv[index]);
  254.                     UseResFile(resRefNum);
  255.                     myETHdl = NewHandle(sizeof(etabData));
  256.                     BlockMove((Ptr)&myETAB, *myETHdl, sizeof(etabData));
  257.                     myEFHdl = NewHandle(efntSize = (2 + 1 + myEFNT.fName[0]));
  258.                     /* size word plus string's length + 1 for the length byte */
  259.                     BlockMove((Ptr)&myEFNT, *myEFHdl, efntSize);
  260.                     AddResource(myETHdl, 'ETAB', 1004, "");
  261.                     AddResource(myEFHdl, 'EFNT', 1003, "");
  262.                     CloseResFile(resRefNum);
  263.                     numFiles++;                    /* keep track of files we've fixed    */
  264.                 } /* if skinny.fdType... */    
  265.             }
  266.         } /* else  */
  267.     }    /* for */
  268.     printf("%d files changed\n", numFiles);        /* report back on our accomplishments    */
  269. }
  270.     
  271.     
  272.